package KK.GameBase
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	
	public class SpriteBase extends Sprite
	{
		static public const TOP:int=1;
		static public const SCENE:int=2;
		static public const BACKGROUND:int=3;
		static public const POP:int=4;
		static public const STAGE:int=0;
		
		private var pop:Sprite;
		private var top:Sprite;
		private var scene:Sprite;
		private var background:Sprite;
		public function SpriteBase()
		{
			//TODO: implement function
			super();
			init();
		}
		private function init():void
		{
			pop=new Sprite();
			top=new Sprite();
			scene=new Sprite();
			background=new Sprite();
			
			this.addChild(background);
			this.addChild(scene);
			this.addChild(top);
			this.addChild(pop);
			
		}
		/*
		**添加场景对象
		**mc添加的对象，添加的位置
		*/
		public function addChildScene(child:DisplayObject,numChild:int=SCENE):void{
			switch(numChild)
			{
				case  POP:
					pop.addChild(child);
					break;
				case TOP:
					top.addChild(child);
					break;
				case SCENE:
					scene.addChild(child);
					break;
				case BACKGROUND:
					background.addChild(child);
					break;
				case STAGE:
					addChild(child);
					break;
				default:
					break;
			}
		}
		/*
		**移除对象
		**mc对象，numchild场景位置
		*/
		public function removeChildScene(child:DisplayObject,numChild:int=SCENE):void{
			switch(numChild)
			{
				case  POP:
					pop.removeChild(child);
					break;
				case TOP:
					top.removeChild(child);
					break;
				case SCENE:
					scene.removeChild(child);
					break;
				case BACKGROUND:
					background.removeChild(child);
					break;
				case STAGE:
						removeChild(child);
						break;
				default:
					break;
			}
		}
		/**
		 * 设置场景位置
		 * 被修改的对象 mc,numChild 被修改的场景位置,index = -1 是顶层
		 */		
		public function setSceneIndex(child:DisplayObject,numChild:int=SCENE,index:int=-1):void{
			if(index==-1)
			{
				switch(numChild)
				{
					case POP:
						index=getTop(pop);
						break;
					case TOP:
						index=getTop(top);
						break;
					case SCENE:
						index=getTop(scene);
						break;
					case BACKGROUND:
						index=getTop(background);
						break;
				}
			}
			switch(numChild)
			{
				case POP:
					pop.setChildIndex(child,index);
					break;
				case TOP:
					top.setChildIndex(child,index);
					break;
				case SCENE:
					scene.setChildIndex(child,index);
					break;
				case BACKGROUND:
					background.setChildIndex(child,index);
					break;
				default:
					break;
			}
		}
		/**
		 * 获取mc顶层
		 * child 是场景
		 * 返回顶层位置 
		 */
		public function getTop(child:DisplayObjectContainer):int{
			return child.numChildren-1>=0?child.numChildren-1:0;
		}
	}
}